package org.example.yuewen.solution23;

import org.example.sword2offer.primary.Q36_FindFirstCommonNode;

/**
 *  * @author: lynn
 *  * @description:  链表翻转
 *  * @date: 2021/4/10 21:43
 *  * @version 1.0.0
 *  
 */
public class ReverseListNode {
    public static void main(String[] args) {
        ListNode listNode=new ListNode(1);
        listNode.next=new ListNode(2);
        listNode.next.next=new ListNode(3);
        listNode.next.next.next=new ListNode(4);
        listNode.next.next.next.next=new ListNode(5);
        System.out.println(Reverse(listNode));
    }

    /**
     *         curNode
     *    pre--> 1-->2-->3-->4-->5
     *
     */
    private static ListNode Reverse(ListNode head){
        if (head==null||head.next==null){
            return head;
        }

        ListNode preNode =null;
        ListNode curNode=head;
        while (curNode!=null){
            ListNode nextNode=curNode.next;
            curNode.next=preNode; //fixme 单个链表翻转的核心
            preNode=curNode;
            curNode=nextNode;
        }
        return preNode;
    }
    private static class ListNode{
        int val;
        ListNode next;
        public ListNode(int val){
            this.val=val;
        }


    }
}
